package com.example.asyncktask;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	private HttpUtil httpUtil;
	private LiShiAdapter liShiAdapter;
	private ListView lishi;
	private ProgressBar progressBar4;
	private String url = "http://www.mygold.cc/news.xml";
	private List<LiShiBean> listBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}
	
	private void init() {

		httpUtil = new HttpUtil();

		progressBar4 = (ProgressBar) this.findViewById(R.id.progressBar4);

		Button back = (Button) this.findViewById(R.id.backbtn);
		back.setOnClickListener(this);

		lishi = (ListView) this.findViewById(R.id.list_lishi);
//		birectSeeding();
		doTask();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.backbtn:
			finish();
			break;

		default:
			break;
		}
	}

	class LiShiAdapter extends BaseAdapter {

		private Context context;
		private List<LiShiBean> listview;

		public LiShiAdapter(Context context, List<LiShiBean> listview) {
			this.context = context;
			this.listview = listview;
		}

		public int getCount() {
			return listview.size();
		}

		public Object getItem(int position) {
			return listview.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			LiShiBean lishiBean = listview.get(position);
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.lishi_item, parent, false);
			}
			TextView biaoti = get(convertView, R.id.biaoti_text);
			TextView shijian = get(convertView, R.id.shijian_text);
			TextView neirong = get(convertView, R.id.neirong_text);
			TextView laiyuan = get(convertView, R.id.laiyuan_text);
			biaoti.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
			biaoti.setText(lishiBean.getNewsTitle());
			shijian.setText(lishiBean.getNewsTime());
			neirong.setText(lishiBean.getNewsContent());
			laiyuan.setText(lishiBean.getAuthor());
			return convertView;
		}
	}

	@SuppressWarnings("unchecked")
	public static <T extends View> T get(View view, int id) {
		SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
		if (viewHolder == null) {
			viewHolder = new SparseArray<View>();
			view.setTag(viewHolder);
		}
		View childView = viewHolder.get(id);
		if (childView == null) {
			childView = view.findViewById(id);
			viewHolder.put(id, childView);
		}
		return (T) childView;
	}

	public void birectSeeding() {
		new Thread(new Runnable() {

			public void run() {
				try {
					Message msg = handler.obtainMessage();
					msg.obj = httpUtil.getLiShiResult_post(url);
					handler.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
					progressBar4.setVisibility(View.GONE);
				}
			}
		}).start();
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {
			ArrayList<LiShiBean> temp = (ArrayList<LiShiBean>) msg.obj;
			liShiAdapter = new LiShiAdapter(MainActivity.this, temp);
			lishi.setAdapter(liShiAdapter);
			liShiAdapter.notifyDataSetChanged();
			progressBar4.setVisibility(View.GONE);
		};
	};
	
	private void doTask(){
		LiShiAsycnTask liShiAsycnTask = new LiShiAsycnTask();
		liShiAsycnTask.execute();
	}

	class LiShiAsycnTask extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			try {
				listBean = httpUtil.getLiShiResult_post(url);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			liShiAdapter = new LiShiAdapter(MainActivity.this, listBean);
			lishi.setAdapter(liShiAdapter);
			liShiAdapter.notifyDataSetChanged();
			progressBar4.setVisibility(View.GONE);
		}
	}

}